package br.com.gsc.controller;

import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.gsc.model.tableMapping.Person;
import br.com.gsc.model.tableMapping.Post;
import br.com.gsc.model.tableMapping.Product;
import br.com.gsc.model.tableMapping.Topic;
import br.com.gsc.repository.objRepos.PersonRepository;
import br.com.gsc.repository.objRepos.PostRepository;
import br.com.gsc.repository.objRepos.ProductRepository;
import br.com.gsc.repository.objRepos.TopicRepository;

@Controller
@RequestMapping("/{companyId}/produto/{productId}/topico/**")
public class TopicController {

	@Autowired
	private PostRepository postRepo;
	@Autowired
	private TopicRepository topRepo;
	@Autowired
	private PersonRepository perRepo;
	@Autowired
	private ProductRepository proRepo;
	@Autowired
	private TopicControllerHelper topHelper;
	@Autowired
	@Qualifier("authentication")
	private Authentication auth;
	

	
	@RequestMapping(value="/{companyId}/produto/{productId}/topico/{topicId}", method=RequestMethod.GET)
	public ModelAndView showPage(@PathVariable("topicId") int id,
								 @PathVariable("companyId") long compId,
			 					 @PathVariable("productId") long prodId){
		ModelAndView model = new ModelAndView("topico");
		Topic topic = topRepo.findTopicByID((long) id);
		model.addObject("topic",topic);	
		Post post = new Post();
		post.setPerson(topic.getPerson());
		post.setTopic(topic);
		model.addObject("post",post);
		model.addObject("logged",auth.getName());
		model.addObject("company", compId);
		model.addObject("product", prodId);
		return model;
	}
	
	@RequestMapping(value="/{companyId}/produto/{productId}/topico/{topicId}", method=RequestMethod.PUT)
	public String editTopic(@PathVariable("topicId") int id,
			  				@PathVariable("companyId") long compId,
			  				@PathVariable("productId") long prodId){
		Topic topic = topRepo.findTopicByID((long) id);
		//TODO
		return "redirect:/"+compId+"/produto/"+prodId+".html";
	}
	
	@RequestMapping(value="/{companyId}/produto/{productId}/topico/{topicId}", method=RequestMethod.DELETE)
	public String deleteTopic(@PathVariable("topicId") int id,
			 				  @PathVariable("companyId") long compId,
			 				  @PathVariable("productId") long prodId){
		Topic topic = topRepo.findTopicByID((long) id);
		topRepo.removeTopic(topic);
		return "redirect:/"+compId+"/produto/"+prodId+".html";
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value="/{companyId}/produto/{productId}/topico/addtopic")
	public ModelAndView addtopic(@ModelAttribute("topic") @Valid Topic topic, BindingResult result,
								 @PathVariable("companyId") long compId,
								 @PathVariable("productId") long prodId){
		
		if(result.hasErrors()){			
			return topHelper.initInternPage();
		}			
		
		if(auth.isAuthenticated() && !auth.getName().equals("anonymousUser")){			
			ModelAndView model = new ModelAndView("redirect:/"+compId+"/produto/"+prodId+".html");
			Topic newTopic = new Topic();
			newTopic.setTopicTitle(topic.getTopicTitle());
			newTopic.setTopicContent(topic.getTopicContent());
			newTopic.setTopicType(topic.getTopicType());
			newTopic.setCreatedIn(new Date());
			Person p = perRepo.findPersonByID(auth.getName());
			newTopic.setPerson(p);
			Product pro = proRepo.findTProductByID(prodId);
			newTopic.setProduct(pro);
			topRepo.addTopic(newTopic);
			return model;
		}else{
			ModelAndView model = new ModelAndView("forward:/loginform.html");
			return model;
		}
	}
	
}
